package com.mujin.keji.collectionsystem.utils;

import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;


import com.bumptech.glide.load.Transformation;
import com.mujin.keji.collectionsystem.utils.ImageLoader.base.BaseLoaderConfig;
import com.mujin.keji.collectionsystem.utils.ImageLoader.base.ImageRouter;
import com.mujin.keji.collectionsystem.utils.ImageLoader.base.OnLoadImageListener;
import com.mujin.keji.collectionsystem.utils.ImageLoader.glide.GlideConfig;

/**
 * @author 作者：weishixiong
 * @date 创建时间：2018/5/2
 * @corporation 公司：mujin
 * @desception
 */

public class ImageUtils {
    /**
     * 加载网络图片
     * @param context
     * @param url
     * @param iv
     */
    public static void loadImage(Context context, String url, ImageView iv) {
        ImageRouter.getInstance()
                .with(context)
                .path(url)
                .target(iv)
                .loadImage();
    }

    /**
     * 加载本地图片
     * @param context
     * @param resId
     * @param iv
     */
    public static void loadImage(Context context, int resId, ImageView iv) {
        BaseLoaderConfig config = new GlideConfig()
                .setResId(resId)
                .setContext(context)
                .setImageView(iv);
        ImageRouter.getInstance()
                .config(config)
                .loadImage();
    }

    public static void loadImage(Context context, int resId, ImageView iv, Transformation transformation) {
        BaseLoaderConfig config = new GlideConfig()
                .setTransformation(transformation)
                .setResId(resId)
                .setContext(context)
                .setImageView(iv);
        ImageRouter.getInstance()
                .config(config)
                .loadImage();
    }

    /**
     * 加载网路图片
     * @param context
     * @param url
     * @param iv
     * @param placeHolder 替换图
     * @param errorImg 错误图片
     */
    public static void loadImage(Context context, String url, ImageView iv, int placeHolder, int errorImg) {
        if (TextUtils.isEmpty(url)) {
            iv.setImageResource(placeHolder);
            return;
        }
        //默认为GlideLoader
        BaseLoaderConfig config = new GlideConfig()
                .setContext(context)
                .setPlaceHolder(placeHolder)
                .setErrorImg(errorImg)
                .setImagePath(url)
                .setImageView(iv);
        ImageRouter.getInstance()
                .config(config)
                .loadImage();
    }

    public static void loadImage(Context context, String url, ImageView iv, int placeHolder, int errorImg,
                                 Transformation transformation) {
        if (TextUtils.isEmpty(url)) {
            iv.setImageResource(placeHolder);
            return;
        }
        //默认为GlideLoader
        BaseLoaderConfig config = new GlideConfig()
                .setTransformation(transformation)
                .setContext(context)
                .setPlaceHolder(placeHolder)
                .setErrorImg(errorImg)
                .setImagePath(url)
                .setImageView(iv);
        ImageRouter.getInstance()
                .config(config)
                .loadImage();
    }

    public static void loadImage(Context context, String url, ImageView iv, int placeHolder, int errorImg,
                                 OnLoadImageListener listener) {
        if (TextUtils.isEmpty(url)) {
            iv.setImageResource(placeHolder);
            return;
        }
        //默认为GlideLoader
        BaseLoaderConfig config = new GlideConfig()
                .setContext(context)
                .setPlaceHolder(placeHolder)
                .setErrorImg(errorImg)
                .setImagePath(url)
                .setImageView(iv)
                .setListener(listener);
        ImageRouter.getInstance()
                .config(config)
                .loadImage();
    }

    public static void loadImage(Context context, String url, ImageView iv,
                                 Transformation transformation, OnLoadImageListener listener) {
        //默认为GlideLoader
        BaseLoaderConfig config = new GlideConfig()
                .setTransformation(transformation)
                .setContext(context)
                .setImagePath(url)
                .setImageView(iv)
                .setListener(listener);
        ImageRouter.getInstance()
                .config(config)
                .loadImage();
    }

    public static void loadImage(Context context, String url, ImageView iv, OnLoadImageListener listener) {
        BaseLoaderConfig config = new GlideConfig()
                .setContext(context)
                .setImagePath(url)
                .setImageView(iv)
                .setListener(listener);
        ImageRouter.getInstance()
                .config(config)
                .loadImage();
    }

    public static void clearCache(Context context) {
        ImageRouter.getInstance().with(context).clearMemCache();
    }
}
